/**
 * Package:com.goldcitynet.service.impl;
 * Id: SceneComponentRelationServiceImpl.java,$v 1.3 2008/05/05 10:47:35 $yangchuan Exp 
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.engine.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.afteryuan.engine.model.Component;
import com.afteryuan.engine.model.Scene;
import com.afteryuan.engine.model.SceneComponentRelation;
import com.afteryuan.engine.service.SceneComponentRelationService;
import com.afteryuan.dao.HibernateEntityDao;
import com.afteryuan.util.PaginationBean;

/**
 * SceneComponentRelationServiceImpl
 * <p/>
 * <p><a href="SceneComponentRelationServiceImpl.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author: hanj $</a>
 * @version $Revision: 1.2 $
 */
public class SceneComponentRelationServiceImpl extends HibernateEntityDao<SceneComponentRelation> implements SceneComponentRelationService {

	private static Log log = LogFactory.getLog(SceneComponentRelationServiceImpl.class);
	
	public List<SceneComponentRelation> getListByCondition(String sceneid,String order,Object... values) {
		List<SceneComponentRelation> sceneComponents = null;
		StringBuffer hql = new StringBuffer("from SceneComponentRelation where 1=1 ");
		if(sceneid!=null&&!sceneid.equals("")){
			hql.append(" and scene.id='"+sceneid+"'");
		}
		if(order!=null&&!order.equals("")){
			hql.append(" order by "+order);
		}
		if(values.length>0&&values[0] instanceof PaginationBean)
			sceneComponents = createList(hql.toString(),values[0]);
		else
			sceneComponents = createList(hql.toString());
		
        return sceneComponents;
	}

    public List<SceneComponentRelation> getListByCondition(String sceneid,String order,Integer[] notLoad,Object... values) {
		List<SceneComponentRelation> sceneComponents = null;
		StringBuffer hql = new StringBuffer("from SceneComponentRelation where 1=1 ");
		if(sceneid!=null&&!sceneid.equals("")){
			hql.append(" and scene.id='"+sceneid+"'");
		}
         hql.append("and component.func not in (") ;
        for(int i = 0 ;i<notLoad.length;i++){
            if(i>0)hql.append(",");
            hql.append(notLoad[i]);
        }
        hql.append(")");
        if(order!=null&&!order.equals("")){
			hql.append(" order by "+order);
		}
         log.debug(hql.toString());
        if(values.length>0&&values[0] instanceof PaginationBean)
			sceneComponents = createList(hql.toString(),values[0]);
		else
			sceneComponents = createList(hql.toString());

        return sceneComponents;
	}

    public List<SceneComponentRelation> getRefreshPositionInScene(String sceneId){
        List<SceneComponentRelation> sceneComponents = null;
        StringBuffer hql = new StringBuffer("from SceneComponentRelation where 1=1 and component.func=15");
        if(sceneId != null){
            hql.append(" and scene.id='").append(sceneId).append("'");
            sceneComponents = createList(hql.toString());
        }
        return sceneComponents;
    }

    public List<SceneComponentRelation> getTransportInScene(String sceneid, Object... values) throws Exception {
		List<SceneComponentRelation> sceneComponents = null;
		StringBuffer hql = new StringBuffer("from SceneComponentRelation where 1=1 and component.func=1");
		if(sceneid!=null&&!sceneid.equals("")){
			hql.append(" and scene.id='"+sceneid+"'");
		}
		if(values.length>0&&values[0] instanceof PaginationBean)
			sceneComponents = createList(hql.toString(),values[0]);
		else
			sceneComponents = createList(hql.toString());
		
		return sceneComponents;
	}
	
	public void removeAll(Scene scene) {
	    String hql = "delete from SceneComponentRelation where scene.id=?";
	    Query q = createQuery(hql,scene.getId());
	    q.executeUpdate();
	}
	
	public void removeComponents(String id) {
		String hql = "delete from SceneComponentRelation where component.id=?";
	    Query q = createQuery(hql,id);
	    q.executeUpdate();
		
	}

    /**
     * @see com.afteryuan.engine.service.SceneComponentRelationService#listByScene(com.afteryuan.engine.model.Scene, Integer, Object[])
     */
    public List<SceneComponentRelation> listByScene(Scene scene,Integer layer, Object... values) {
        StringBuffer hql = new StringBuffer("from SceneComponentRelation where 1=1");
        if(scene == null || scene.getId() == null)return null;
        hql.append(" and scene.id='").append(scene.getId()).append("'");
        if(layer > -1)hql.append(" and layer=").append(layer);
        hql.append(" order by depth");
        if(values.length>0&&values[0] instanceof PaginationBean)
            return createList(hql.toString(),values);
        return createList(hql.toString());
    }

    public SceneComponentRelation getBySceneAndDepth(String sceneid, Integer deep) {
        String sql = "from SceneComponentRelation  scr where scr.scene.id='"+sceneid+"' and scr.depth="+deep;
        return createList(sql).get(0);
    }
    
    public List listByComponent(Component comp) {
    	String hql = "select distinct(scr.scene.id) from SceneComponentRelation scr where scr.component.id='" + comp.getId() + "'"; //   	
//    	SQLQuery query = this.getSession().createSQLQuery(hql);
//    	query.executeUpdate();
    	Query query = createQuery(hql);
    	
    	return query.list();
    }
}